package com.rocsea.robot.utils;

import com.rocsea.robot.model.entity.*;

import java.util.Objects;

import static com.rocsea.robot.constant.SqlBuildConstant.*;

/**
 * @Author RocSea
 * @Date 2023/1/12
 */
public final class MysqlExtBuilderUtils {
    public static final String VALUES_PREFIX = "VALUES (";
    public static final String VALUES_SUFFIX = ");";
    public static final String SEMICOLON = ";";
    public static final String INSERT_ETL_SYNC_CHECK_CONFIG_PREFIX = "INSERT INTO `etl_sync`.`etl_sync_check_config` " +
            "(`source_service_module`, " +
            "`source_schema_name`, `source_table_name`, `source_join_condition`, `source_delete_type`, `source_where_condition`, " +
            "`source_table_type`, `target_service_module`, `target_schema_name`, `target_table_name`, `target_join_condition`, " +
            "`target_delete_type`, `target_where_condition`, `target_table_type`) ";

    public static final String INSERT_DML_SUBSCRIBE_CONFIG_PREFIX = "INSERT INTO `etl_sync`.`dml_subscribe_config` " +
            "(`target_service_module`, `source_table_topic`, `target_schema_name`, `target_table_name`, `operation_type`, " +
            "`enable_status`, `dml_consume_type`, `dml_source_sharding_field`)";
    public static final String UPDATE_STEP_ATTRIBUTE_PREFIX =
            "update `kettle_resource`.`r_step_attribute` set VALUE_NUM=";
    public static final String UPDATE_STEP_ATTRIBUTE_SUFFIX =
            " where ID_STEP_ATTRIBUTE=";

    private MysqlExtBuilderUtils() {
    }

    public static String buildEtlSyncCheckConfig(EtlSyncCheckConfigDO checkConfigDO) {
        return INSERT_ETL_SYNC_CHECK_CONFIG_PREFIX + VALUES_PREFIX + checkConfigDO.getSourceServiceModule() +
                COMMA + ifAddSingleQuote(checkConfigDO.getSourceSchemaName()) +
                COMMA + ifAddSingleQuote(checkConfigDO.getSourceTableName()) +
                COMMA + ifAddSingleQuote(checkConfigDO.getSourceJoinCondition()) +
                COMMA + checkConfigDO.getSourceDeleteType() +
                COMMA + ifAddSingleQuote(checkConfigDO.getSourceWhereCondition()) +
                COMMA + checkConfigDO.getSourceTableType() +
                COMMA + checkConfigDO.getTargetServiceModule() +
                COMMA + ifAddSingleQuote(checkConfigDO.getTargetSchemaName()) +
                COMMA + ifAddSingleQuote(checkConfigDO.getTargetTableName()) +
                COMMA + ifAddSingleQuote(checkConfigDO.getTargetJoinCondition()) +
                COMMA + checkConfigDO.getTargetDeleteType() +
                COMMA + ifAddSingleQuote(checkConfigDO.getTargetWhereCondition()) +
                COMMA + checkConfigDO.getTargetTableType() + VALUES_SUFFIX + LINE_FEED;
    }

    private static String ifAddSingleQuote(String s) {
        if (Objects.nonNull(s)) {
            return SINGLE_QUOTE + s + SINGLE_QUOTE;
        }
        return null;
    }

    public static String buildSwitchDatasource(StepAttributeDO stepAttributeDO, Long newDatasourceId) {
        return UPDATE_STEP_ATTRIBUTE_PREFIX + newDatasourceId + UPDATE_STEP_ATTRIBUTE_SUFFIX + stepAttributeDO.getIdStepAttribute()
                + SEMICOLON + LINE_FEED;
    }

    public static String buildDmlSubscribeConfig(DmlSubscribeConfigDO dmlSubscribeConfigDO) {
        return INSERT_ETL_SYNC_CHECK_CONFIG_PREFIX + VALUES_PREFIX + dmlSubscribeConfigDO.getTargetServiceModule() +
                COMMA + ifAddSingleQuote(dmlSubscribeConfigDO.getSourceTableTopic()) +
                COMMA + ifAddSingleQuote(dmlSubscribeConfigDO.getTargetSchemaName()) +
                COMMA + ifAddSingleQuote(dmlSubscribeConfigDO.getTargetTableName()) +
                COMMA + dmlSubscribeConfigDO.getOperationType() +
                COMMA + dmlSubscribeConfigDO.getEnableStatus() +
                COMMA + dmlSubscribeConfigDO.getDmlConsumeType() +
                COMMA + ifAddSingleQuote(dmlSubscribeConfigDO.getDmlSourceShardingField()) + VALUES_SUFFIX + LINE_FEED;
    }
}
